package org.linlinjava.litemall.core.notify;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.JacksonUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/*
 * 腾讯云短信服务
 */
public class Z253SmsSender implements SmsSender {
    private final Log logger = LogFactory.getLog(Z253SmsSender.class);


    private String url;
    private String account;
    private String password;


    public void setInitData(String url,String account,String password) {
        this.url=url;
        this.account=account;
        this.password=password;

    }

    @Override
    public SmsResult send(String phone, String content) {
        try {
            Map map = new HashMap();;

            map.put("account",account);//API账号
            map.put("password",password);//API密码
            map.put("msg",content);//短信内容
            map.put("phone",phone);//手机号
            map.put("report","true");//是否需要状态报告
            logger.debug(JacksonUtil.toJson(map));
            String result=sendSmsByPost(url,JacksonUtil.toJson(map));
            logger.debug(result);

            SmsResult smsResult = new SmsResult();
            smsResult.setSuccessful(true);
            smsResult.setResult(result);
            return smsResult;
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }

        SmsResult smsResult = new SmsResult();
        smsResult.setSuccessful(false);
        return smsResult;
    }

    @Override
    public SmsResult sendWithTemplate(String phone, String templateId, String[] params) {
        try {
            Map map = new HashMap();;

            map.put("account",account);//API账号
            map.put("password",password);//API密码
            map.put("msg",String.format(templateId,params));//短信内容
            map.put("phone",phone);//手机号
            map.put("report","true");//是否需要状态报告
            logger.debug(JacksonUtil.toJson(map));
            String result=sendSmsByPost(url,JacksonUtil.toJson(map));
            logger.debug(result);

            SmsResult smsResult = new SmsResult();
            smsResult.setSuccessful(true);
            smsResult.setResult(result);
            return smsResult;
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }

        SmsResult smsResult = new SmsResult();
        smsResult.setSuccessful(false);
        return smsResult;
    }

    public static String sendSmsByPost(String path, String postContent) throws IOException {

        URL url = null;

        url = new URL(path);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setConnectTimeout(10000);
        httpURLConnection.setReadTimeout(10000);
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);
        httpURLConnection.setRequestProperty("Charset", "UTF-8");
        httpURLConnection.setRequestProperty("Content-Type", "application/json");
        httpURLConnection.connect();
        OutputStream os=httpURLConnection.getOutputStream();
        os.write(postContent.getBytes("UTF-8"));
        os.flush();
        StringBuilder sb = new StringBuilder();
        int httpRspCode = httpURLConnection.getResponseCode();
        if (httpRspCode == HttpURLConnection.HTTP_OK) {
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
            String line = null;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            br.close();
            return sb.toString();
        }

        return null;
    }

}
